#!/bin/bash

# 日志文件路径
LOG_FILE="record.log"

# 检查并创建日志文件
if [ ! -f "$LOG_FILE" ]; then
    touch "$LOG_FILE"
fi

# 获取当前日期和时间
current_time() {
    date "+%Y-%m-%d %H:%M:%S"
}

# 获取进程 ID
get_pid() {
    pgrep -f qlpmsp
}

# 获取 CPU 占用率
get_cpu_usage() {
    local pid=$1
    ps -p $pid -o %cpu | awk 'NR>1 {print}'
}

# 获取内存使用情况
get_memory_usage() {
    local pid=$1
    ps -p $pid -o %mem | awk 'NR>1 {print}'
}

# 获取运行时间
get_runtime() {
    local pid=$1
    ps -p $pid -o etime | awk 'NR>1 {print}'
}

# 主循环
while true; do
    pid=$(get_pid)
    if [ -z "$pid" ]; then
        echo "$(current_time) - Process qlpmsp not found" >> "$LOG_FILE"
    else
        cpu_usage=$(get_cpu_usage $pid)
        memory_usage=$(get_memory_usage $pid)
        runtime=$(get_runtime $pid)
        
        echo "$(current_time) - PID: $pid, CPU Usage: $cpu_usage%, Memory Usage: $memory_usage%, Runtime: $runtime" >> "$LOG_FILE"
    fi
    
    sleep 5
done
